﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace CJJCH.Web.Manage
{
    public partial class NormType : PowerBase
    {
        CJJCH.BLL.T_INF_NormType bll_type = new BLL.T_INF_NormType();
        CJJCH.Model.T_INF_NormType model = new Model.T_INF_NormType();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.form1.DefaultButton = "btnSearch";
            if (!IsPostBack)
            {
                InitRequest();
                Repeater_DataItem();
            }
        }

        protected void InitRequest()
        {
            if (Request.QueryString["keyword"] != null)
            {
                lblkeyword.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keyword"].ToString());
            }
            txtKeyword.Text = lblkeyword.Text.Trim();
        }
        protected void Repeater_DataItem()
        {
            IList<CJJCH.Model.T_INF_NormType> list = new List<CJJCH.Model.T_INF_NormType>();
            string normType = LTP.Common.StringClass.EncodeString(lblkeyword.Text.Trim());
            list = bll_type.GetNormTypeList(normType);
            this.rptNormType.DataSource = list;
            this.rptNormType.DataBind();
            if (list.Count == 0)
            {
                litMessage.Text = "<table width=100%><tr style=height:18px bgcolor=#FFFFFF><td align='center'><font color=red>对不起，系统没有检索出有关信息！<font></td></tr></table>";
            }
            else
            {
                litMessage.Text = "";
            }
        }
        protected void rptNormType_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((TextBox)e.Item.FindControl("txtTypeName")).Text = ((CJJCH.Model.T_INF_NormType)e.Item.DataItem).TypeName;
                ((TextBox)e.Item.FindControl("txtOrder")).Text = ((CJJCH.Model.T_INF_NormType)e.Item.DataItem).OrderNum.ToString();
                ((LinkButton)e.Item.FindControl("lbtnEdit")).CommandArgument = ((CJJCH.Model.T_INF_NormType)e.Item.DataItem).ID.ToString();
                ((LinkButton)e.Item.FindControl("lbtnDel")).Attributes.Add("onclick", "javascript:if(confirm('确定删除该类别吗？')){return true;}else{return false;}");
                ((LinkButton)e.Item.FindControl("lbtnDel")).CommandArgument = ((CJJCH.Model.T_INF_NormType)e.Item.DataItem).ID.ToString();
            }
        }
        protected string CheckInput()
        {
            StringBuilder error = new StringBuilder();
            if (txtNormType.Text.Trim() == "")
            {
                error.Append("请输入类别名称！！\\n");
            }
            if (txtOrderNum.Text.Trim() == "")
            {
                error.Append("请输入排序号！！\\n");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(txtOrderNum.Text.Trim()))
            {
                error.Append("排序号必须是数字！！\\n");
            }
            return error.ToString();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.lblkeyword.Text = LTP.Common.StringClass.EncodeString(this.txtKeyword.Text.Trim());
            Repeater_DataItem();
        }
        
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnEdit_Click(object sender, EventArgs e)
        {
            StringBuilder error = new StringBuilder();
            LinkButton linb = (LinkButton)sender;
            int Index = ((RepeaterItem)(linb.NamingContainer)).ItemIndex;
            TextBox txtTypeName = (TextBox)rptNormType.Items[Index].FindControl("txtTypeName");
            TextBox txtOrder = (TextBox)rptNormType.Items[Index].FindControl("txtOrder");
            if (txtTypeName.Text.Trim() == "")
            {
                error.Append("请输入类别名称！！\\n");
            }
            if (txtOrder.Text.Trim() == "")
            {
                error.Append("请输入排序号！！\\n");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(txtOrder.Text.Trim()))
            {
                error.Append("排序号必须是数字！！\\n");
            }

            if (!string.IsNullOrEmpty(error.ToString()))
            {
                LTP.Common.MessageBox.Show(this, error.ToString());
                return;
            }
            model.ID = int.Parse(linb.CommandArgument);
            model.TypeName = LTP.Common.StringClass.EncodeString(txtTypeName.Text.Trim());
            model.OrderNum = int.Parse(txtOrder.Text.Trim());
            model.ManagerID = SessionManagerID;
            string res = bll_type.EditNormType(model);
            LTP.Common.MessageBox.ShowAndRedirect(this, res, "NormType.aspx?keyword=" + LTP.Common.StringClass.EncodeUrl(lblkeyword.Text.Trim()));
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtbDel_Click(object sender, EventArgs e)
        {
            LinkButton linb = (LinkButton)sender;
            string res = "";
            res = bll_type.DelNormType(Convert.ToInt32(linb.CommandArgument));
            if (string.IsNullOrEmpty(res.Trim()))
            {
                Response.Redirect("NormType.aspx?keyword=" + LTP.Common.StringClass.EncodeUrl(lblkeyword.Text.Trim()));
            }
            else
            {
                LTP.Common.WindowClass.WindowBack(res);
            }
        }

        protected void lbtnAdd_Click(object sender, EventArgs e)
        {
            string error = "";
            error = CheckInput();
            if (!string.IsNullOrEmpty(error))
            {
                LTP.Common.WindowClass.WindowBack(error);
            }
            else
            {
                model.TypeName = LTP.Common.StringClass.EncodeString(this.txtNormType.Text.Trim());
                model.OrderNum = int.Parse(this.txtOrderNum.Text.Trim());
                model.ManagerID = SessionManagerID;
                string res = bll_type.AddNormType(model);
                LTP.Common.MessageBox.ShowAndRedirect(this, res, "NormType.aspx?keyword=" + LTP.Common.StringClass.EncodeUrl(lblkeyword.Text.Trim()));
            }
        }
    }
}